package com.oocl.ita.sakura.handler;

import java.io.ObjectOutputStream;
import java.util.ArrayList;

import com.oocl.ita.sakura.client.Client;
import com.oocl.ita.sakura.forwarder.Forwarder;
import com.oocl.ita.sakura.ioutil.NetIOUtil;
import com.oocl.ita.sakura.protocol.Protocol;
import com.oocl.ita.sakura.server.IMServer;

public class LogoutHandler implements IHandler {

	@Override
	public void handle(Protocol protocol) {
		/*
		 * 1. logout 请求 清除ClientOOSMap中的logoutClient 此报文中什么字段是有意义的？ msgType from
		 * 从from字段获取 Client， 然后remove
		 * 
		 * 2. 更新所有Client的状态 NetIOUtil.sendAll(protocol)
		 * 
		 * 3. 这里用不到某个Client的oos， 而是所有的
		 */

		// debug
		System.out.println("LogoutHandler : " + Thread.currentThread().getName());

		// pre-process
		// IMServer.ClientOOSMap 移除用户
		Client logoutClient = (Client) protocol.getParams().get("from");
		IMServer.removeClient(logoutClient);

		// 封装报文
		Protocol retProtocol = new Protocol("syn");
		retProtocol.encapsulate("syn", new ArrayList(IMServer.getClientOosMap().keySet()));

		// syn同步
		NetIOUtil.send(retProtocol, new Client("all"));
		System.out.println("LogoutHanlder : " + retProtocol);
		System.out.println("LogoutHandler Done");

	}

}
